package com.hangxiang.approval.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hangxiang.approval.common.ActionGroupPageCommon;
import com.hangxiang.approval.common.ActiongroupInsertCommon;
import com.hangxiang.approval.common.ActiongroupUpdateCommon;
import com.hangxiang.approval.common.IntegerCommon;
import com.hangxiang.approval.dao.ActiongroupDao;
import com.hangxiang.approval.dao.CcPersonDao;
import com.hangxiang.approval.dao.ReviewerDao;
import com.hangxiang.approval.entity.*;
import com.hangxiang.approval.enums.ResultStatusEnum;
import com.hangxiang.approval.exception.MyException;
import com.hangxiang.approval.service.ActiongroupService;
import com.hangxiang.approval.vo.ActiongroupVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * (Actiongroup)表服务实现类
 *
 * @author makejava
 * @since 2020-11-23 18:07:46
 */
@Service("actiongroupService")
public class ActiongroupServiceImpl extends ServiceImpl<ActiongroupDao, Actiongroup> implements ActiongroupService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private ActiongroupDao actiongroupDao;

    @Resource
    private ReviewerDao reviewerDao;

    @Resource
    private CcPersonDao ccPersonDao;
    /**
     * 通过ID查询单条数据
     *
     * @param common 主键
     * @return 实例对象
     */
    @Override
    public ActiongroupVo queryById(IntegerCommon common) {
        ActiongroupVo vo = new ActiongroupVo();
        Actiongroup actiongroup = actiongroupDao.selectById(common.getValue());
        BeanUtils.copyProperties(actiongroup, vo);
        return vo;
    }


    /**
     * 新增数据
     *
     * @param actiongroupInsertCommon 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Integer insertActiongroup(List<ActiongroupInsertCommon> actiongroupInsertCommon) {
//        QueryWrapper<Actiongroup> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(Actiongroup::getActionId, 0);
//        if (actiongroupInsertCommon.getTempId() != null) {
//            queryWrapper.lambda().eq(Actiongroup::getTempId, actiongroupInsertCommon.getTempId());
//        }
        actiongroupInsertCommon.forEach(actionGroup -> {
            Actiongroup action=actiongroupDao.selectByTempId(actionGroup.getTempId());

            if (action==null) {
                Actiongroup actiongroup = new Actiongroup();
                //如果为空就是没有抄送流程
                actiongroup.setTempId(actionGroup.getTempId());
                actiongroup.setActionName(actionGroup.getActionName());
                actiongroupDao.insert(actiongroup);
                //审核人
                Reviewer reviewer=new Reviewer();
                reviewer.setReviewerUser(actionGroup.getReviewerUser());
                reviewer.setActionGroupId(actiongroup.getActionId());
                reviewer.setReviewerName(actionGroup.getReviewerName());
                reviewer.setActionGroupId(actiongroup.getActionId());
                reviewer.setCreateTime(new Date());
                reviewerDao.insert(reviewer);

                //获取审核人id插入抄送人
                if(actionGroup.getCcPersonCommonList().size()!=0) {
                    actionGroup.getCcPersonCommonList().forEach(ccPersonCommon -> {
                        CcPerson ccPerson = new CcPerson();
                        ccPerson.setReviewerId(reviewer.getId());
                        ccPerson.setCcPersonId(ccPersonCommon.getCcPersonId());
                        ccPerson.setActionGroupId(actiongroup.getActionId());
                        ccPerson.setCcPersonName(ccPersonCommon.getCcPersonName());
                        ccPerson.setCreateTime(new Date());
                        ccPersonDao.insert(ccPerson);
                    });
                }
            }else{
                //审核人
                Reviewer reviewer=new Reviewer();
                reviewer.setReviewerUser(actionGroup.getReviewerUser());
                reviewer.setActionGroupId(action.getActionId());
                reviewer.setReviewerName(actionGroup.getReviewerName());
                reviewer.setCreateTime(new Date());
                reviewerDao.insert(reviewer);
                //获取审核人id插入抄送人
                if(actionGroup.getCcPersonCommonList().size()!=0) {
                    actionGroup.getCcPersonCommonList().forEach(ccPersonCommon -> {
                        CcPerson ccPerson = new CcPerson();
                        ccPerson.setReviewerId(reviewer.getId());
                        ccPerson.setCcPersonId(ccPersonCommon.getCcPersonId());
                        ccPerson.setActionGroupId(action.getActionId());
                        ccPerson.setCcPersonName(ccPersonCommon.getCcPersonName());
                        ccPerson.setCreateTime(new Date());
                        ccPersonDao.insert(ccPerson);
                    });
                }

            }

        });

        return null;
    }

    /**
     * 修改数据
     *
     * @param actiongroupUpdateCommon 实例对象
     * @return 实例对象
     */
    @Override
    public Integer updateActiongroup(ActiongroupUpdateCommon actiongroupUpdateCommon) {
        Actiongroup actiongroup = new Actiongroup();
        BeanUtils.copyProperties(actiongroupUpdateCommon, actiongroup);
        actiongroupDao.updateById(actiongroup);
        return actiongroup.getActionId();
    }


}